package cn.lizemin.demo;

import cn.lizemin.constant.RpcConstant;
import cn.lizemin.service.UserApi;
import cn.lizemin.service.UserController;
import cn.lzm.RpcApplication;
import cn.lzm.config.RegistryConfig;
import cn.lzm.config.RpcConfig;
import cn.lzm.model.ServiceMetaInfo;
import cn.lzm.registry.LocalRegistry;
import cn.lzm.registry.Registry;
import cn.lzm.registry.RegistryFactory;
import cn.lzm.server.tcp.VertxTcpServerV5;

/**
 * @author lzm
 * @Date 2024/11/6
 * @description 服务提供者示例
 */
public class TcpProviderExample {

    /**
     * 启动服务器
     */
    public static void main(String[] args) {
        // RPC框架初始化
        RpcApplication.init();
        // 注册服务，其实就是controller接口的类名 todo: 这里可能还需要优化，因为所有接口都需要在这里手动注册太麻烦了
        String serviceName = UserApi.class.getName();
        LocalRegistry.register(serviceName, UserController.class);

        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();

        Registry registry = RegistryFactory.getInstance(registryConfig.getType());
        ServiceMetaInfo info = new ServiceMetaInfo();
        info.setName(serviceName);
        info.setVersion(RpcConstant.DEFAULT_SERVICE_VERSION);
        info.setHost(rpcConfig.getServerHost());
        info.setPort(rpcConfig.getServerPort());
        try {
            registry.register(info);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 启动tcp服务器
        VertxTcpServerV5 tcpServer = new VertxTcpServerV5();
        tcpServer.start(rpcConfig.getServerPort());
    }

}
